﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-25 19:49:05
 * 功能：下拉列表控制器，大厅界面专用
**************************************************************************************/

using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class DropdownCtrl : MonoBehaviour, IPointerClickHandler
    {
        private CustomDropdown m_Dropdown;
        private Image m_Arrow;
        [SerializeField] Sprite m_ArrowUp;
        [SerializeField] Sprite m_ArrowDown;

        private void Awake()
        {
            m_Dropdown = GetComponent<CustomDropdown>();
            m_Arrow = transform.Find("Arrow").GetComponent<Image>();
            m_Dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
            m_Dropdown.OnDestroyDropdownList += OnDestroyDropdownList;
            m_Dropdown.AddOptions(GameDataMgr.Instance.MapInfo.countries.Values.Select(x => x.name).ToList());
        }

        private void OnDestroyDropdownList()
        {
            m_Arrow.sprite = m_ArrowDown;
        }

        private void OnDropdownValueChanged(int idx)
        {
            var list = GameDataMgr.Instance.MapInfo.countries.Values.ToList();
            if (!list[idx].isUnlocked)
            {
                UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("Unlocked after completing all cities in the previous country."));
                m_Dropdown.SetValueWithoutNotify(0);
            }
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            m_Arrow.sprite = m_ArrowUp;
        }
    }
}